package com.hgc.ch02;

import java.util.concurrent.CountDownLatch;

/**
 * Created by 01367627 on 2019/7/8.
 */
public class CountDownLatchExample {

    private static CountDownLatch latch = new CountDownLatch(50);

    private static class MyRunnable implements Runnable{

        @Override
        public void run() {
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "开始运行！");
        }
    }

    public static void main(String[] args) {
        //与多线程一起测试
        for(int i =0;i<50;i++){
            new Thread(new MyRunnable(),"t" + i).start();
            latch.countDown();
        }
    }

}
